package com.david.auth.pojo.util;

import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.i18n.LocaleContextHolder;

/**
 * 国际化工具类
 */
@Configuration
public class LocaleUtils {

    private static MessageSource messageSource;

    public LocaleUtils(MessageSource messageSource){
        LocaleUtils.messageSource=messageSource;
    }

    /**
     * 获取单个国际化翻译值
     */
    public static String get(String msgKey) {
        try {
            if(null == msgKey){
                return "";
            }
            return messageSource.getMessage(msgKey, null, LocaleContextHolder.getLocale());
        } catch (Exception e) {
            return msgKey;
        }
    }
}
